package com.bsoft.mvpmy.ui.home;

import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bsoft.mvpmy.R;
import com.bsoft.mvpmy.adapter.SimpleTreeListViewAdapter;
import com.bsoft.mvpmy.base.BaseActivity;
import com.bsoft.mvpmy.base.MyApplication;
import com.bsoft.mvpmy.model.ResultBean.FileBean;
import com.bsoft.mvpmy.utils.Node;
import com.bsoft.mvpmy.utils.adapter.TreeListViewAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * User: ZRJ
 * Date: 2016/12/2
 * Email: 471564517@qq.com
 * Description:
 */
public class HomeActivity extends BaseActivity<HomeContract.Presenter> implements HomeContract.View {


    @Bind(R.id.ll_menu)
    LinearLayout llMenu;
    @Bind(R.id.ll_user)
    LinearLayout llUser;
    @Bind(R.id.ll_set)
    LinearLayout llSet;
    @Bind(R.id.ll_exit)
    LinearLayout llExit;
    @Bind(R.id.ll_home)
    LinearLayout llHome;
    @Bind(R.id.tv_name)
    TextView tvName;
    private FrameLayout fl_content;//容器布局
    private FirstFragment firstFragment; //首页
    private ListView mTree;
    private LinearLayout ll_menu, ll_user, ll_set, ll_exit;
    private SimpleTreeListViewAdapter<FileBean> mAdapter;
    private List<FileBean> mDatas;
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        findId();


        inItView();
        //------------
        initdata();
        //------------
        initEvent();
        //引入侧滑菜单
        initDrawlayout();

    }


    private void initdata() {
        String data = "[\n" +
                "    {\n" +
                "        \"id\": \"2\",\n" +
                "        \"name\": \"医疗\",\n" +
                "        \"parent_id\": \"0\",\n" +
                "        \"url\": \"\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\": \"3\",\n" +
                "        \"name\": \"公卫\",\n" +
                "        \"parent_id\": \"0\",\n" +
                "        \"url\": \"\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\": \"4\",\n" +
                "        \"name\": \"门诊费用\",\n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\": \"5\",\n" +
                "        \"name\": \"门诊业务量\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"6\",\n" +
                "        \"name\": \"就诊流向\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"7\",\n" +
                "        \"name\": \"住院费用\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"8\",\n" +
                "        \"name\": \"住院业务量\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"9\",\n" +
                "        \"name\": \"1+1+1签约\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"10\",\n" +
                "        \"name\": \"1+1+1签约情况\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"11\",\n" +
                "        \"name\": \"运行机制\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"12\",\n" +
                "        \"name\": \"标化工作量\", \n" +
                "        \"parent_id\": \"3\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"13\",\n" +
                "        \"name\": \"财政补偿\", \n" +
                "        \"parent_id\": \"3\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"14\",\n" +
                "        \"name\": \"门急诊与住院收入情况\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"15\",\n" +
                "        \"name\": \"门诊工作负荷\", \n" +
                "        \"parent_id\": \"2\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }, \n" +
                "    {\n" +
                "        \"id\": \"16\",\n" +
                "        \"name\": \"公共卫生\", \n" +
                "        \"parent_id\": \"3\",\n" +
                "        \"url\": \"http://www.baidu.com/\"\n" +
                "    }\n" +
                "]";

        try {
            JSONArray array = new JSONArray(data);
            mDatas = new ArrayList<>();
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);
                FileBean node = new FileBean();
                node.setId(object.getInt("id"));
                node.setpId(object.getInt("parent_id"));
                node.setLabel(object.getString("name"));
                node.setDesc(object.getString("url"));
                mDatas.add(node);
            }
            try {
                mAdapter = new SimpleTreeListViewAdapter<FileBean>(mTree, this,
                        mDatas, 0);
                mTree.setAdapter(mAdapter);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    /**
     * 组件查找
     */
    private void findId() {

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mTree = (ListView) findViewById(R.id.id_listview);
    }

    /**
     * 添加数据
     */
    private void inItView() {
        firstFragment = new FirstFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content, firstFragment);
        transaction.commit();
        Bundle bundle = new Bundle();
        bundle.putString("url", "");
        firstFragment.setArguments(bundle);
    }


    private void initEvent() {
        mAdapter.setOnTreeNodeClickListener(new TreeListViewAdapter.OnTreeNodeClickListener() {
            @Override
            public void onClick(Node node, int position) {
                if (node.isLeaf()) {

                    firstFragment = new FirstFragment();
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    transaction.replace(R.id.fl_content, firstFragment);
                    transaction.commit();
                    Bundle bundle = new Bundle();
                    bundle.putString("url", node.getDesc());
                    firstFragment.setArguments(bundle);
                    mAdapter.set_chooseId(node.getId());
                    tvName.setText(node.getName());
                    drawer.closeDrawer(mTree);
                }
            }
        });
    }

    private void initDrawlayout() {
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }

    @Override
    public void onBackPressed() {
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void homesuccess() {

    }

    @Override
    public void hideLoading() {

    }


    private AlertDialog dialog;

    @OnClick({R.id.ll_menu, R.id.ll_user, R.id.ll_set, R.id.ll_exit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_menu:
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(mTree);
                } else {
                    drawer.openDrawer(mTree);
                }
                break;
            case R.id.ll_user:
                break;
            case R.id.ll_set:
                break;
            case R.id.ll_exit:
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("退出程序");
                builder.setMessage("是否退出程序");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        MyApplication.getInstance().exit();

                    }
                });

                dialog = builder.create();
                dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
                //dialog.getWindow().getDecorView().setSystemUiVisibility(View.VISIBLE);

                break;
        }
    }

    @OnClick(R.id.ll_home)
    public void onClick() {
        firstFragment = new FirstFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content, firstFragment);
        transaction.commit();
        Bundle bundle = new Bundle();
        bundle.putString("url", "");
        firstFragment.setArguments(bundle);
        drawer.closeDrawer(mTree);
        tvName.setText("首页");
        mAdapter.set_chooseId(-1);

    }
}
